import { Component, OnInit, Input } from '@angular/core';
import { EventService } from '../../shared/service/event.service';
import { AlertController, Platform } from '@ionic/angular';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { HttpService } from 'src/app/shared/service/http.service';
@Component({
  selector: 'app-way-skills',
  templateUrl: './way-skills.component.html',
  styleUrls: ['./way-skills.component.scss'],
})
export class WaySkillsComponent implements OnInit {

  @Input() tagData: any;
  cssStr: any = {};
  constructor(
    private eventService: EventService,
    private alertController: AlertController,
    private httpService: HttpService,
    private platform: Platform,
    private appGlobalService: AppGlobalService
  ) { }

  ngOnInit() {
    // console.log(this.tagData);
    if (this.tagData.data.cssStyle) {
      // tslint:disable-next-line:prefer-const
      let cssStyles = this.tagData.data.cssStyle.split(",");
      // tslint:disable-next-line:prefer-for-of
      for (let i = 0; i < cssStyles.length; i++) {
        // tslint:disable-next-line:prefer-const
        let cssStrs = cssStyles[i].split(":");
        this.cssStr[cssStrs[0]] = cssStrs[1];
      }
    }

    if (this.tagData.data.content) {
      this.tagData.data.content.forEach(data => {
        if (data.cssStyle) {
          const cssStyles = data.cssStyle.split(",");
          const childCss = {};
          // tslint:disable-next-line:prefer-for-of
          for (let i = 0; i < cssStyles.length; i++) {
            // tslint:disable-next-line:prefer-const
            let cssStrs = cssStyles[i].split(":");
            childCss[cssStrs[0]] = cssStrs[1];
          }
          data.childCss = childCss;
        }
      });
    }
  }

  // tslint:disable-next-line:use-life-cycle-interface
  ngAfterViewInit() {
    if (this.tagData.data.htmlObj) {
      const contentHtml = document.createElement("div");
      // document.getElementById("addHtml").innerHTML = this.tagData.data.content[0];
      contentHtml.innerHTML = this.tagData.data.content[0].label;
      document.getElementById("addHtml").appendChild(contentHtml);
      console.log(contentHtml);
      localStorage.setItem("voiceText", document.getElementById("addHtml").innerText);

    }
  }

  copyToPaste(item) {
    try {
      if (item.style === 'phone') {
        this.presentAlertConfirm(item.value);
      } else if (item.style === 'weixin') {
        this.copyUrl(item.value);
        // 唤起微信
      } else if (item.style === 'message') {
        // 复制将要发送的短信信息
        // this.copyUrl(item.value);
        const nickName = localStorage.getItem('currentNickName') ? localStorage.getItem('currentNickName') : '';

        const platformStr = localStorage.getItem('platform');

        const specialSymbol = platformStr === 'ios' ? '&' : '?';

        // sms:10086?body=您好，我是蜂喔，给您推荐了信息，请及时到蜂喔APP查看哦
        document.location.href = "sms:" + item.value + specialSymbol + 'body=您好，我是蜂喔科技的' + nickName + '，给您推荐了信息，请及时到蜂喔APP查看哦';
      }
    } catch (error) {
      console.log('error:' + JSON.stringify(error));
    }
  }

  async presentAlertConfirm(mobile, name?) {
    const msg = (name ? name + ' ' : '') + mobile;
    const alert = await this.alertController.create({
      header: this.tagData.nickName,
      message: msg,
      buttons: [
        {
          text: "取消",
          role: "cancel",
          cssClass: "secondary"
        },
        {
          text: "拨打",
          handler: () => {
            this.httpService.changeCallNumber(mobile);
          }
        }
      ]
    });

    await alert.present();
  }

  copyUrl(str) {
    this.appGlobalService.copyString(str, '号码，请到微信通知');
  }


}
